//
//  HtmlParase.m
//  NetTest
//
//  Created by mac on 12-5-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HtmlParase.h"
#import "NSString+Regular.h"
#import "TicketResultService.h"
#import "TicketResult.h"
#import "ASIFormDataRequest.h"

#define URLStr @"http://zst.cjcp.com.cn/cjwklsf_tianjin/view/klsf_jb.php?searchType=href&countnumber=100000&typeId=&xingId=8"
#define URLPost @"http://zst.cjcp.com.cn/cjwklsf_tianjin/view/klsf_jb.php"

@implementation HtmlParase

+(NSString*) urlstring:(NSString*)strurl{
    NSURL *url = [NSURL URLWithString:strurl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);    
    NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
    
    //NSLog(@" html = %@",retStr);
    
    return retStr;
}

+(NSString*) urlstring{
    return [self urlstring:URLStr];
}



+ (void)htmlToModel:(NSString *)strhtml {
    NSString *regstr = @"<td class=\'z_bg_05\'>\\w{11}</td><td class=\'z_bg_13\'>(\\w{2}\\s{0,1})*</td>";
    NSMutableArray *arr=[strhtml substringByRegular:regstr];
    
    if (arr!=nil&&[arr count]>0) {
        
        NSString *prereg=@"\\w{11}";
        NSString *backreg=@"(\\w{2}\\s{0,1}){8}";
        
        TicketResultService *service=[[TicketResultService alloc] init];
        [[Sqlite3Helper Instance] openDB];
        for (NSString *sub in arr) {
            
            TicketResult* r=[[[TicketResult alloc] init] autorelease];
            
            NSMutableArray* prearr=[sub substringByRegular:prereg];
            
            if (prearr!=nil&&[prearr count]>0) {
                r.sectionID=(NSString*)[prearr objectAtIndex:0];
            }
            else{
                continue;
            }
            
            NSMutableArray *backarr=[sub substringByRegular:backreg];
            if (backarr!=nil&&[backarr count]>0) {
                r.result=[backarr objectAtIndex:0];
            }
            else{
                continue;
            }
            
            if([service isExist:r.sectionID]){
                continue;
            }
            
            r.type=[NSNumber numberWithInt:1];
            
            [service addModel:r];
            
        }
        [[Sqlite3Helper Instance] closeDB];
        
        [service release];
    }
}

+(void)getTickResult{
    
    NSString *strhtml=[self urlstring];
    
    [self htmlToModel:strhtml];
}

+(void)getPostResult:(NSString*)startqi{
	ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:URLPost]];
    
    NSLog(@"start qi = %@",startqi);
    
	[request setPostValue:startqi forKey:@"startqi"];
    [request setPostValue:@"20990101001" forKey:@"endqi"];
    [request setPostValue:@"qihao" forKey:@"searchType"];//网页的中的搜索方式
    [request startSynchronous];
    
    NSData* data = [request responseData];
    
    if (data==nil) {
        FCLOG(@"has not data");
    }
    else{
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);    
        NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
        FCLOG(@"html = %@",retStr); 
        [self htmlToModel:retStr];
    }
}

@end
